/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2024-07-06
 * Time: 23:21
 */



import java.util.*;

public class Muster {
    // 定义并集存储对象
    private Set<Character> unite = new LinkedHashSet<Character>();
    // 定义交集存储对象
    private Set<Character> intersection = new LinkedHashSet<Character>();
    // 定义a-b差集存储对象
    private Set<Character> differenceA2B = new LinkedHashSet<Character>();
    // 定义b-a差集存储对象
    private Set<Character> differenceB2A = new LinkedHashSet<Character>();

    /**
     * 计算每个集合
     *

     */
    public Muster(String muster1, String muster2) {
//把非26个英文字母全替换成空串
        muster1 = muster1.replaceAll("[^a-z]", "");
        muster2 = muster2.replaceAll("[^a-z]", "");
        //循环第一个集合
        for (Character temp : muster1.toCharArray()) {
            //添加并集
            unite.add(temp);
            if (muster2.indexOf(temp) >= 0) {
                //如果集合2中存在当前元素，在交集中追加记录
                intersection.add(temp);
            } else {
                //如果集合2中不存在当前元素，在第一个集合与第二个集合的差集添加记录
                differenceA2B.add(temp);
            }
        }
        //循环第二个集合
        for (Character temp : muster2.toCharArray()) {
            //添加并集
            unite.add(temp);
            if (muster1.indexOf(temp) >= 0) {
                //如果集合1中存在当前元素，在交集中追加记录
                intersection.add(temp);
            } else {
                //如果集合1中不存在当前元素，在第二个集合与第一个集合的差集添加记录
                differenceB2A.add(temp);
            }
        }
    }

    public static void main(String[] args) {
        char[] str1 = new char[100];
        char[] str2 = new char[100];
        char[] str3 = new char[100];
        char temp;
        String string1, string2, replace = null;
        Scanner scan = new Scanner(System.in);

        System.out.println("请输入第一串字符");
        string1 = scan.nextLine();
        str1 = string1.toCharArray();
        replace = string1;

        for (int i = 0; i < string1.length(); i++) {
            for (int j = string1.length() - 1; j > i; j--) {
                if (str1[j] <= str1[j - 1]) {
                    temp = str1[j];
                    str1[j] = str1[j - 1];
                    str1[j - 1] = temp;
                }
            }
        }
        String i = String.valueOf(str1);

        System.out.println("请输入第二串字符");
        string2 = scan.nextLine();
        str2 = string2.toCharArray();
        replace = string2;

        for (int a = 0; a < string2.length(); a++) {
            for (int j = string2.length() - 1; j > a; j--) {
                if (str2[j] <= str2[j - 1]) {
                    temp = str2[j];
                    str2[j] = str2[j - 1];
                    str2[j - 1] = temp;
                }
            }
        }
        String j = String.valueOf(str2);//将 char 变量 c 转换成字符串


        Muster muster = new Muster(i, j);
        str3 = muster.getUnite().toCharArray();
        replace = muster.getUnite();

        for (int a = 0; a < muster.getUnite().length(); a++) {
            for (int k = muster.getUnite().length() - 1; k > a; k--) {
                if (str3[k] <= str3[k - 1]) {
                    temp = str3[k];
                    str3[k] = str3[k - 1];
                    str3[k - 1] = temp;
                }
            }
        }
        String k = String.valueOf(str3);


        System.out.println("Set1∪Set2=" + k);
        //输出交集
        System.out.println("Setl∩Set2=" + muster.getIntersection());
        //第一个集合与第二个集合的差集
        System.out.println("Set1-Set2=" + muster.getDifferenceA2B());
        //输出第二个集合与第一个集合的差集
        System.out.println("Set2-Set1=" + muster.getDifferenceB2A());


    }

    public String getUnite() {
        return getStringBySet(unite);
    }

    public String getIntersection() {
        return getStringBySet(intersection);
    }

    public String getDifferenceA2B() {
        return getStringBySet(differenceA2B);
    }

    public String getDifferenceB2A() {
        return getStringBySet(differenceB2A);
    }

    private String getStringBySet(Set<Character> set) {
        StringBuffer result = new StringBuffer();
        for (Character c : set) {
            result.append(c);
        }
        return result.toString();
    }

}
